<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">展开/折叠</button>
                <button class="layui-btn layui-btn-normal" id="btn-add" lay-event="add">添加</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-xs" lay-event="up">^</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="addChild"> 添加</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="down">v</a>
</script>

<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        var treeIndex = 0;
        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'symId',
            treePidName: 'pid',
            elem: '#munu-table',
            url: '/menu/showAll.do',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'title', minWidth: 100, title: '菜单名称'},
                {
                    field: 'icon', title: '标识', width: 80, align: 'center', templet: function (d) {
                        return '<i class="' + d.icon + '"></i>';
                    }
                },
                {field: 'href', minWidth: 150, title: '菜单url'},
                {field: 'sort', width: 80, align: 'center', title: '排序'},
                {
                    filed: 'flag', width: 80, align: 'center', title: '状态', templet: function (d) {
                        if (d.flag == 0)
                            return "使用中";
                        else if (d.flag == 1)
                            return "已禁用";
                    }
                },
                {field: 'remark', minWidth: 150, title: '备注'},
                {templet: '#auth-state', minwidth: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });
        $('#btn-expand').click(function () {
            if (treeIndex == 0) {
                treetable.foldAll('#munu-table');
                treeIndex = 1;
            } else if (treeIndex == 1) {
                treetable.expandAll('#munu-table');
                treeIndex = 0;
            }
        });
        $('#btn-add').click(function () {
            var index = layer.open({
                title: '添加菜单',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['40%', '80%'],
                content: './menu-add.html',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        });
        // $('#btn-fold').click(function () {
        //     treetable.foldAll('#munu-table');
        // });
        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'addChild') {
                if (data.level == 4) {
                    layer.msg("菜单请不要超过四层", {
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                } else {
                    var index = layer.open({
                        title: '添加菜单',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['40%', '80%'],
                        content: '/page/management/power/menu-addChild.html?sid=' + data.symId,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                }
            } else if (layEvent === 'del') {
                alert(data.icon);
                layer.msg('删除' + data.symId);
            } else if (layEvent === 'edit') {
                layer.msg('修改' + data.symId);
                var index = layer.open({
                    title: '添加菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['40%', '80%'],
                    content: '/page/management/power/menu-edit.html?sid=' + data.symId,
                });
                $(window).on("resize", function () {

                });
            } else if (layEvent === 'up') {
                if (data.sort == 1 || data.sort == 0)
                    layer.msg('已经是最前序号');
                else {
                    $.post("/menu/up.do", {id: data.symId},
                        function (data) {
                            if (data.code == 0) {
                                layer.msg(data.msg, {
                                        time: 1000//2秒关闭（如果不配置，默认是3秒）
                                    },
                                    function () {
                                        window.location.reload();
                                    });
                            } else if (data.code == 1)
                                layer.msg(data.msg);
                        }, "json");
                }
            }else if (layEvent === 'down'){
                var sort = data.sort;
                var id = data.symId;
                var pid = data.pid;
                $.post("/menu/sortEnd.do",{pid:pid},function (data){
                    if (sort == data.data)
                        layer.msg('已经是最末序号');
                    else {
                        $.post("/menu/down.do", {id: id},
                            function (data) {
                                if (data.code == 0) {
                                    layer.msg(data.msg, {
                                            time: 1000//2秒关闭（如果不配置，默认是3秒）
                                        },
                                        function () {
                                            window.location.reload();
                                        });
                                } else if (data.code == 1)
                                    layer.msg(data.msg);
                            }, "json");
                    }
                },"json")
            }
        });
    });
</script>
</body>
</html>